<template>
  <section class="app-inner">
    <el-card class="header-card">
      <el-form :inline="true" :model="queryParams" size="small" label-width="84px">
        <el-form-item label="年级组长">
          <el-select  v-model="queryParams.leaderNo"
            clearable
            placeholder="请选择年级组长"
            filterable
          >
            <el-option
              v-for="item in teacherList"
              :key="item.number"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="年级名称">
          <el-input clearable placeholder="请输入年级名称" v-model="queryParams.name"></el-input>
        </el-form-item>

        <el-form-item label="入学年份">
        <el-date-picker
        style="width: 100%"
        v-model="queryParams.year"
        type="year"
        placeholder="选择入学年份"
        size="small"
        >
        </el-date-picker>
        </el-form-item>

      <el-form-item label="状态"  prop="status" class="line">
          <el-select v-model="queryParams.status"
            clearable
            placeholder="请选择状态"
            filterable
            style="width: 100%"
          >
            <el-option
              v-for="item in statusList"
              :key="item.id"
              :label="item.status"
              :value="item.id"
            ></el-option>
          </el-select>
      </el-form-item>

        <el-form-item>
          <el-button v-if="checkAuthority('/select')" type="primary" @click="searchData">查询</el-button>
          <el-button v-if="checkAuthority('/sepGradeInfo/add')" @click="handleAdd" type="primary" icon="plus" size="small">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="序号" width="100">
          <template slot-scope="scope">
            {{pageSize*(pageNum-1)+scope.$index+1}}
          </template>
        </el-table-column>        <!-- <el-table-column prop="eqptAddress" label="设备位置">
          <template slot-scope="scope">
            {{ scope.row.eqptAddress.replace(/null/g, '未知') }}
          </template>
        </el-table-column> -->
        <el-table-column prop="name" label="年级名称"></el-table-column>
        <el-table-column prop="period" label="学段"></el-table-column>
        <el-table-column prop="year" label="入学年份"></el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
          {{
              scope.row.status === 0
              ? '作废'
              : scope.row.status === 1
              ? '正常'
              : scope.row.status === 2
              ? '毕业'
              : ''
          }}
          </template>
        </el-table-column>
        <el-table-column prop="leaderNo" label="年级组长"  width="300">
          <template slot-scope="scope">
          <el-select  v-model="scope.row.leaderNo"
            clearable
            :disabled="!checkAuthority('/update/leader')"
            placeholder="请选择年级组长"
            filterable
            @change="leaderChange($event,scope.row)"
            size="small"
            style="width:150px"
          >
            <el-option
              v-for="item in teacherList"
              :key="item.number"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
            <el-button v-if="checkAuthority('/sepGradeInfo/addLeader')" @click="addTeachInfo" type="text" size="small" style="margin-left:20px">新增年级组长</el-button>
            </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="checkAuthority('/sepGradeInfo/update')" @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
            <el-button v-if="checkAuthority('/sepGradeInfo/delete')" @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="total, prev, pager, next"
        @current-change="pageHandleChange"
        :current-page="pageNum"
        :page-size="pageSize"
        :total="totalPageNum"
      ></el-pagination>
    </el-card>
    <grade-info-modal ref="gradeInfoModal" @ok="modalOk"></grade-info-modal>
    <add-teach-info :infoData="infoData" :deptData="departments" :addTeachForm="addTeachForm" :data="addTeachFormData" @closeaddTeachForm="addTeacher"></add-teach-info>
  </section>
</template>

<script>
import addTeachInfo from './components/addTeachInfo'
import gradeInfoModal from './modules/gradeInfoModal'
import { queryDeptListBySchoolNo } from '@/views/children/basic/js/teacherInfo'
import { getGradeInfoList, deleteGradeInfo, teacherList, teacherChange } from './js/gradeInfo'
import { sepDataDicInfoQuery } from '@/views/children/basic/js/controllerInfo'
import moment from 'moment'
import { checkAuthority } from '@/utils/btnPermission' // 页面按钮权限
export default {
  name: 'gradeInfo',
  components: { gradeInfoModal, addTeachInfo },
  provide () {
    return {
      handleSearch: function () {

      }
    }
  },
  data () {
    return {
      pageNum: 1,
      pageSize: 10,
      totalPageNum: 1,
      tableData: [],
      queryParams: {},
      eqptTypeList: [],
      teacherList: [],
      statusList: [{ id: 0, status: '作废' }, { id: 1, status: '正常' }, { id: 2, status: '毕业' }],
      addTeachFormData: {},
      infoData: {},
      // 部门
      departments: [],
      // 新增
      addTeachForm: {
        flag: false,
        title: ''
      },
      old: ''
    }
  },
  mounted () {
    this.sepDataDicInfoQuery()
    this.getGradeInfoList()
    this.getTeacherList()
    this.queryDeptListBySchoolNo()
  },
  methods: {
    checkAuthority,
    // 新增教职工信息
    addTeachInfo () {
      this.addTeachForm.flag = true
      this.addTeachForm.title = '新增教职工信息'
      this.addTeachFormData = {
        isCreateAccount: 1,
        gender: 0,
        cardType: '1',
        nationality: '156',
        positionType: '0'
      }
    },
    // 查询按钮
    searchData () {
      this.pageNum = 1
      this.getGradeInfoList()
    },
    // 年级组长选择
    leaderChange (event, row) {
      this.$confirm('您确定选择当前年级组长, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.teacherChange({ id: row.id, leaderNo: row.leaderNo }, event, row)
        })
        .catch(() => {
          // row.leaderNo = this.old
          this.getGradeInfoList()
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      console.log(row)
    },
    async teacherChange (object, event, row) {
      let res = await teacherChange(object)
      if (res.code === 0) {
        // this.old = event
        // row.leaderNo = event
        console.log(row)
        this.$message.success('修改年级组长成功!')
      } else {
        // row.leaderNo = this.old
        this.$message.warning(res.msg)
      }
      this.getGradeInfoList()
    },
    // 新增
    handleAddLeader () {
      this.$refs.teachInfoModal.isAdd = true
      this.$refs.teachInfoModal.add()
    },
    // 新增
    handleAdd () {
      this.$refs.gradeInfoModal.isAdd = true
      this.$refs.gradeInfoModal.add()
    },
    modalOk () {
      this.getGradeInfoList()
    },
    // 分页
    pageHandleChange (page) {
      this.pageNum = page
      this.getGradeInfoList()
    },
    async getGradeInfoList () {
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      // this.queryParams,
      if (this.queryParams.year) {
        this.queryParams.year = moment(this.queryParams.year).format('YYYY').toString()
      }
      const data = Object.assign({}, this.queryParams, params)
      console.log(data)
      let res = await getGradeInfoList(data)
      console.log(res)
      this.tableData = res.data.records
      this.totalPageNum = res.data.total
    },
    async  getTeacherList () {
      let res = await teacherList({})
      console.log(res)
      this.teacherList = res.data
      this.$refs.gradeInfoModal.getTeacherList()
    },
    // 编辑
    handleEdit (row) {
      this.$refs.gradeInfoModal.isAdd = false
      this.$refs.gradeInfoModal.edit(row)
    },
    // 删除
    handleDelete (row) {
      this.$confirm('您确定删除当前年级, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.deleteFun(row)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async deleteFun (row) {
      let res = await deleteGradeInfo(row.id)
      if (res.code === 0) {
        if (this.tableData.length === 1 && this.pageNum > 1) {
          this.pageNum--
        }
        this.$message.success('删除成功!')
        this.getGradeInfoList()
      } else {
        this.$message.error(res.msg)
      }
    },
    async sepDataDicInfoQuery () {
      const data = await sepDataDicInfoQuery()
      if (data) {
        this.infoData = data.data
        this.postTypes = data.data
      }
    },
    queryDeptListBySchoolNo () {
      queryDeptListBySchoolNo({ parentNo: '' }).then(res => {
        this.departments = res.data
      })
    },
    addTeacher () {
      this.addTeachForm.flag = false
      this.getTeacherList()
    }
  }
}
</script>

<style scoped>
</style>
